package cn.yto.wdgj.wdgjgptdigitaltool.service.impl;

import cn.yto.base.page.BasePage;
import cn.yto.response.BaseResponse;
import cn.yto.response.DataResult;
import cn.yto.utils.BeanUtil;
import cn.yto.utils.JsonUtil;
import cn.yto.utils.StringUtil;
import cn.yto.wdgj.common.reponse.MutiResponse;
import cn.yto.wdgj.wdgjgptdigitaltool.entity.dto.request.GptCaseMaintenanceRequest;
import cn.yto.wdgj.wdgjgptdigitaltool.entity.dto.request.GptTagMaintenanceRequest;
import cn.yto.wdgj.wdgjgptdigitaltool.entity.dto.response.GptCaseMaintenanceResponse;
import cn.yto.wdgj.wdgjgptdigitaltool.entity.dto.response.GptTagMaintenanceResponse;
import cn.yto.wdgj.wdgjgptdigitaltool.mapper.gpt.GptTagMaintenanceMapper;
import cn.yto.wdgj.wdgjgptdigitaltool.mapper.po.GptCaseMaintenance;
import cn.yto.wdgj.wdgjgptdigitaltool.mapper.po.GptTagMaintenance;
import cn.yto.wdgj.wdgjgptdigitaltool.service.GptTagMaintenanceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GptTagMaintenanceServiceImpl implements GptTagMaintenanceService {
    @Autowired
    private GptTagMaintenanceMapper gptTagMaintenanceMapper;
    public DataResult<BaseResponse> delTagCaseMaintenance(Long tagId) {
        StringBuilder logBuilder = new StringBuilder("【根据条件删除数据】caseId request=[ " + tagId + " ]，");
        try{
            int i = gptTagMaintenanceMapper.deleteById(tagId);
            if (i > 0){
                logBuilder.append("删除成功");
                return DataResult.ok();
            }
            logBuilder.append("删除失败");
        }catch(Exception e){
            log.error("【根据条件删除数据】tagId: {} 删除失败", tagId, e);
            logBuilder.append("删除失败");
        }finally {
            log.info(logBuilder.toString());
        }
        return DataResult.fail("删除失败");
    }

    @Override
    public DataResult<MutiResponse<GptTagMaintenanceResponse>> getAllGptTagMaintenance(GptTagMaintenanceRequest gptTagMaintenanceRequest) {
        StringBuilder logBuilder = new StringBuilder("【根据条件获取列表】paramData request=[ " + JsonUtil.toJson(gptTagMaintenanceRequest) + " ]，");
        LambdaQueryWrapper<GptTagMaintenance> lambdaQueryWrapper = new LambdaQueryWrapper<GptTagMaintenance>();

        if (StringUtil.isNotBlank(gptTagMaintenanceRequest.getIntentName())) {
            lambdaQueryWrapper.in(GptTagMaintenance::getIntentName, gptTagMaintenanceRequest.getIntentName());
        }

        BasePage page = new BasePage();
        page.setCurrentPage(gptTagMaintenanceRequest.getPageNo());
        page.setPageSize(gptTagMaintenanceRequest.getPageSize());
        page.setQueryCount(true);

        Page<GptTagMaintenance> pageResult = PageHelper.startPage(page.getCurrentPage(),
                page.getPageSize(), page.getQueryCount()).doSelectPage(() -> gptTagMaintenanceMapper.selectList(lambdaQueryWrapper));

        int resultNum = CollectionUtils.isEmpty(pageResult.getResult()) ? 0 : pageResult.getResult().size();
        logBuilder.append("接口查询数据量= [ ").append(resultNum).append(" ]，");

        MutiResponse<GptTagMaintenanceResponse> pageResponse = new MutiResponse<>();
        List<GptTagMaintenanceResponse> myWorkOrderResponses = BeanUtil.copyByList(pageResult.getResult(), GptTagMaintenanceResponse.class);

        pageResponse.setItems(myWorkOrderResponses);
        pageResponse.setPageNo(gptTagMaintenanceRequest.getPageNo());
        if (CollectionUtils.isNotEmpty(pageResult.getResult())) {
            pageResponse.setLimit(pageResult.getResult().size());
        } else {
            pageResponse.setLimit(0);
        }
        if (pageResponse.getLimit() < gptTagMaintenanceRequest.getPageSize()) {
            // 查询出来的接口比分页参数少，则认为没有更多数据了
            pageResponse.setMorePage(false);
        } else {
            pageResponse.setMorePage((long) page.getCurrentPage() * page.getPageSize() < pageResult.getTotal());
        }
        pageResponse.setTotal(pageResult.getTotal());
        log.info(logBuilder.toString());
        return DataResult.ok(pageResponse);
    }

    @Override
    public DataResult<BaseResponse> saveGptTagMaintenance(GptTagMaintenanceRequest request) {
        StringBuilder logBuilder = new StringBuilder("【根据条件保存数据】request=[ " + JsonUtil.toJson(request) + " ]，");
        GptTagMaintenance gptTagMaintenance = new GptTagMaintenance();
        BeanUtils.copyProperties(request, gptTagMaintenance);
        try {
            gptTagMaintenance.setCreateTime(new Date());
            int insert = gptTagMaintenanceMapper.insert(gptTagMaintenance);
            if (insert > 0) {
                logBuilder.append("保存成功");
                return DataResult.ok();
            }
            logBuilder.append("保存失败");
        } catch (Exception e) {
            log.error("【根据条件保存数据】request：{} 保存失败", JsonUtil.toJson(request), e);
            logBuilder.append("保存失败");
        } finally {
            log.info(logBuilder.toString());
        }
        return DataResult.fail("保存失败");
        }

    @Override
    public DataResult<BaseResponse> updateGptTagMaintenance(GptTagMaintenanceRequest request) {
        StringBuilder logBuilder = new StringBuilder("【根据条件更新数据】request=[ " + JsonUtil.toJson(request) + " ]，");
        try {
            GptTagMaintenance gptTagMaintenance = new GptTagMaintenance();
            BeanUtils.copyProperties(request, gptTagMaintenance);
            gptTagMaintenance.setUpdateTime(new Date());
            int i = gptTagMaintenanceMapper.updateById(gptTagMaintenance);
            if (i > 0) {
                logBuilder.append("更新成功");
                return DataResult.ok();
            }
            logBuilder.append("更新失败");
            return DataResult.fail("更新失败");
            } catch (Exception e) {
                log.error("【根据条件更新数据】request：{} 更新失败", JsonUtil.toJson(request), e);
                logBuilder.append("更新失败");
            } finally {
                log.info(logBuilder.toString());
            }
        return DataResult.fail("更新失败");
        }
}



